package tf2pickup;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * Represents a list of players, specified by their irc-nickname and 
 * PlayerProperties. Provides some useful methods.
 * @author Philipp Preß
 */
public class Players {

    private HashMap<String, PlayerProperties> list;
    private final int MAXIMUM_SIZE;

    /**
     * Creates a new list of players with the specified maximum size
     * @param size amount of players this pickup will need to start a match.
     */
    public Players(int size) {
        
        this.list = new HashMap<String, PlayerProperties>();
        this.MAXIMUM_SIZE = size;
    }

    /**
     * @return the maximum size of players allowed for this pickup.
     */
    public int getMaximumSize() {
        return MAXIMUM_SIZE;
    }
    
    /**
     * Adds a player with the specified Tf2Class to the pickup.
     * @param nickname irc-nickname of the player who should be added.
     * @param tf2Class Tf2Class the user wants to play.
     * @return the PlayerProperties of the added player.
     */
    public PlayerProperties addPlayer(String nickname, Tf2Class tf2Class) {
        return list.put(nickname, new PlayerProperties(tf2Class));
    }
    
    /**
     * Adds a player with existing PlayerProperties
     * @param nickname irc-nickname of the player who should be added.
     * @param playerProperties PlayerProperties of the player who should be added.
     * @return the PlayerProperties of the added player.
     */
    public PlayerProperties addPlayer(String nickname, PlayerProperties playerProperties){
        return list.put(nickname, playerProperties);
    }
    
    /**
     * Removes a player from the pickup.
     * @param nickname irc-nickname of the player who should be removed.
     * @return the PlayerProperties of the removed player.
     */
    public PlayerProperties removePlayer(String nickname){
        return list.remove(nickname);
    }
    
    /**
     * 
     * @param nickname irc-nickname of the player whose existance should be
     * checked.
     * @return true if a player with the specified nickname is added to the
     * pickup.
     */
    public boolean containsPlayer(String nickname){
        return list.containsKey(nickname);
    }
    
    /**
     * 
     * @return a Set<String> of nicknames from all players that are added to the
     * pickup.
     */
    public Set<String> getNicknames() {
        return list.keySet();
    }
    
    
    /**
     * 
     * @return the amount of players that are currently added to the pickup. 
     */
    public int getPlayersCount() {
        return list.size();
    }
    
    /**
     * @param nickname
     * @return the properties of the specified player.
     */
    public PlayerProperties getPlayerProperties(String nickname){
        return list.get(nickname);
    }
    
    /**
     * Removes all of the players from the pickup. The pickup will be empty
     * after this call returns.
     */
    public void clear() {
        list.clear();
    }
    /**
     * 
     * @param tf2Class
     * @return the number of players that have choosen the specified Tf2Class.
     */
    public int getTf2ClassCount(Tf2Class tf2Class) {
        int count = 0;
        
        for (PlayerProperties p : list.values()) {
            if (p.getTf2Class().equals(tf2Class)){
                count++;
            }
        }
        return count;
    }
    
    /**
     * 
     * @param tf2Class 
     * @return a Set of players that have choosen the specified Tf2Class. 
     */
    public Set<String> getPlayersByTf2Class(Tf2Class tf2Class){
        Set<String> output = new HashSet<String>();
        for (String nickname : list.keySet()){
            if(list.get(nickname).getTf2Class().equals(tf2Class)){
                output.add(nickname);
            }
        }
        return output;
    }
}
